<?php
// +----------------------------------------------------------------------
// |     【DealInfo】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class DealInfo extends Model{
    use SoftDelete;
     protected $deleteTime = false;
	
	/**
	 * 产品一对一关联
	 */
	public function cloth(){
	    return $this->hasOne(ClothMain::class, 'id', 'cloid');
	}
	/**
	 * 订单一对一关联
	 */
	public function deal(){
	    return $this->hasOne(DealMain::class, 'id', 'did')->with(['customer'])->append(['create_time2']);
	}
	
	// 获取列表数据
	public static function listData($id,$where=[],$order="",$more=true,$n_page=0,$limit=50){
		if(is_array($id)){
			$where=[['did','in',$id]];
		}else{
			$where['did']=$id;
		}
		
		if($more){			//最佳供货商信息
			$data=self::alias('i')
			->leftJoin('cloth_main c','i.cloid=c.id')
			->leftJoin('supplier_main s','c.sup_id=s.id')
			->field('i.*,s.name as sup_name,c.breadth,c.square,c.side_type')
			->order($order)
			->where($where);
		}else{
			$data=self::order($order)->where($where);
		}
		
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();

		return $data;
	}
	
	// 获取列表
	public static function history($where=[]){
	    $limit = I('get.limit/d');
		$cusid = I('get.cusid',0);
		$haswhere = [];

		if($number = I('number')){
			$where['DealInfo.number'] = $number;
		}
		if($color = I('color')){
			$where['color'] = $color;
		}
		if($cusid != 0){
			$haswhere['cusid']=$cusid;
		}

		$list = self::hasWhere('deal',$haswhere)->with('deal')->order('create_time','desc')->where($where)->paginate($limit);

	    return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
	}
	
	// 订单明细表匹配颜色
	public static function leftColor(){
		$list=self::withJoin('cloth_color', 'LEFT')->select()->paginate(20);
		p($list->items());
	}
	
	/* 获取器 */
	
	public function getWeightAttr($value){
		$value=unserialize($value);
	    return $value;
	}
}
